package cn.itcast.controller.cargo;

import cn.itcast.base.BaseController;
import cn.itcast.domain.cargo.*;
import cn.itcast.service.cargo.ContractService;
import cn.itcast.service.cargo.ExportProductService;
import cn.itcast.service.cargo.ExportService;
import cn.itcast.service.cargo.PackingService;
import cn.itcast.utils.commons.UtilFuns;
import cn.itcast.vo.ExportProductVo;
import cn.itcast.vo.ExportResult;
import cn.itcast.vo.ExportVo;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.apache.cxf.jaxrs.client.WebClient;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping(value = "/cargo/export")
public class ExportController extends BaseController {

    @Reference
    private ContractService contractService;

    @Reference
    private ExportService exportService;

    @Reference
    private ExportProductService exportProductService;

    @Reference
    private PackingService packingService;

    @RequestMapping(value = "/contractList",name = "合同管理（已上报的合同查询）")
    public String contractList(@RequestParam(defaultValue = "1")int pageNum,@RequestParam(defaultValue = "4")int pageSize){
        ContractExample contractExample = new ContractExample();
        //设置时间排序
        contractExample.setOrderByClause("create_time desc");
        ContractExample.Criteria contractExampleCriteria = contractExample.createCriteria();
        contractExampleCriteria.andCompanyIdEqualTo(companyId);
        contractExampleCriteria.andStateEqualTo(1);

        //分页查询
        PageInfo page = contractService.findAll(contractExample, pageNum, pageSize);
        request.setAttribute("page",page);
        return "cargo/export/export-contractList";
    }

    @RequestMapping(value = "/toExport",name = "生成报运单（到保运页面）")
    public String toExport(String id){
        request.setAttribute("id",id);
        return "cargo/export/export-toExport";
    }

    @RequestMapping(value = "/edit",name = "生成报运单实现")
    public String edit(Export export){
        //设置企业标识
        export.setCompanyId(companyId);
        //设置企业名称
        export.setCompanyName(companyName);
        if (UtilFuns.isEmpty(export.getId())){
            //export.setId(UUID.randomUUID().toString().replace("-",""));
            exportService.save(export);
        }else {
            exportService.update(export);
        }
        return "redirect:/cargo/export/list.do";
    }

    @RequestMapping(value = "/list",name = "出口报运管理（查看生成报运单")
    public String list(@RequestParam(defaultValue = "1")int pageNum,@RequestParam(defaultValue = "4")int pageSize){
        ExportExample exportExample = new ExportExample();
        //设置排序
        exportExample.setOrderByClause("create_time desc");
        //设置提交（企业标识）
        ExportExample.Criteria criteria = exportExample.createCriteria();
        criteria.andCompanyIdEqualTo(companyId);
        //0-草稿 1-已上报 2-已报运 3-装箱 4-委托 5-发票 6-财务
        criteria.andStateBetween(0l,2l);
        //查询报运单
        PageInfo page = exportService.findAll(exportExample,pageNum,pageSize);
        request.setAttribute("page",page);
        //报运单的展示页面
        return "cargo/export/export-list";
    }

    @RequestMapping(value = "/toUpdate",name = "到报运单的修改页面")
    public String toUpdate(String id){
        //带着报运单的数据到修改页面修改
        Export export = exportService.findById(id);
        request.setAttribute("export",export);
        //查询报运单的货物
        ExportProductExample exportProductExample = new ExportProductExample();
        ExportProductExample.Criteria criteria = exportProductExample.createCriteria();
        criteria.andExportIdEqualTo(id);
        //当前报运单的货物
        List<ExportProduct> list = exportProductService.findAll(exportProductExample);
        request.setAttribute("eps",list);
        //到报运单的修改页面
        return "cargo/export/export-update";
    }

    @RequestMapping(value = "/toView",name = "到报运单的查看页面")
    public String toView(String id){
        //带着报运单的数据到查看页面
        Export export = exportService.findById(id);
        request.setAttribute("export",export);
        //到报运单的查看页面
        return "cargo/export/export-view";
    }

    @RequestMapping(value = "/delete",name = "报运单删除")
    public String delete(String id){
        //带着报运单的id进行删除
        exportService.delete(id);
        //到报运单的页面展示最新的数据
        return "redirect:/cargo/export/list.do";
    }

    @RequestMapping(value = "/exportE",name = "出口报运的电子报运")
    public String exportE(String id){
        //1.做报运（组装exportVo调用服务）
        //1.1根据报运单id获取报运单对象   Export:报运表
        Export export = exportService.findById(id);
        //1.2组装exportVo
        ExportVo exportVo = new ExportVo();
        BeanUtils.copyProperties(export,exportVo);
        //设置报运单的id
        exportVo.setExportId(export.getId());
        //1.3获取报运单的货物对象
        ExportProductExample exportProductExample = new ExportProductExample();
        ExportProductExample.Criteria criteria = exportProductExample.createCriteria();
        criteria.andExportIdEqualTo(id);
        List<ExportProduct> productList = exportProductService.findAll(exportProductExample);
        //1.4组装exportProductVo
        List<ExportProductVo> list = new ArrayList();
        if (productList!=null){
            //ExportProduct:保运表下的货物
            for (ExportProduct exportProduct : productList) {
                ExportProductVo exportProductVo = new ExportProductVo();
                BeanUtils.copyProperties(exportProduct,exportProductVo);
                exportProductVo.setExportProductId(exportProduct.getId());
                //组装成集合
                list.add(exportProductVo);
            }
        }
        //1.5把exportProductVo封装给exportVo
        exportVo.setProducts(list);
        //1.6根据指定的提交方式调用服务方法传递指定的参数
        WebClient webClient = WebClient.create("http://localhost:8084/jk/ws/export/user");
        webClient.post(exportVo);

        //2做查询 根据返回的数据修改自己的内容
        WebClient webClient2 = WebClient.create("http://localhost:8084/jk/ws/export/user/"+id);
        ExportResult exportResult = webClient2.get(ExportResult.class);
        //修改自己的报运单数据（状态和备注）以及报运单的货物数据（税）
        exportService.updateByResult(exportResult);
        return "redirect:/cargo/export/list.do";
    }

    @RequestMapping(value = "/submit",name = "提交")
    public String submit(String id){
        Export export = exportService.findById(id);
        export.setState(1);
        exportService.updateState(export);
        return "redirect:/cargo/export/list.do";
    }

    @RequestMapping(value = "/cancel",name = "取消")
    public String cancel(String id){
        Export export = exportService.findById(id);
        export.setState(0);
        exportService.updateState(export);
        return "redirect:/cargo/export/list.do";
    }

    @RequestMapping(value = "/exportPacking",name = "到装箱页面")
    public String exportPacking(String id){
        String[] exportIds = id.split(",");
        for (String exportId: exportIds){
            Export export = exportService.findById(exportId);
            //0-草稿 1-已上报 2-装箱 3-委托 4-发票 5-财务
            //0-草稿 1-已上报 2-已报运 3-装箱 4-委托 5-发票 6-财务
            if (export.getState()!=2){
                request.setAttribute("msg","不是已上报状态无法装箱");
                return "cargo/packing/packing-error";
            }
        }
        request.setAttribute("id",id);
        return "cargo/packing/packing-toPacking";
    }

}
